package common

import java.util.concurrent.TimeUnit

import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport
import helpers.ExtDateTimeFormat.DATETIMEZONE
import org.joda.time.DateTime
import org.joda.time.LocalDate
import play.api.libs.json._

import scala.concurrent.duration.FiniteDuration

trait HttpJsonProtocol extends PlayJsonSupport {

  implicit class ExtendJodaDateTime(dt: DateTime) {

    def millsOfDate: Long = dt.millisOfDay.withMinimumValue.getMillis

    def toDuration: FiniteDuration = FiniteDuration(dt.getMillis, TimeUnit.MILLISECONDS)
  }

  implicit val DateTimeFormat: Format[DateTime] =
    Format(JodaReads.jodaDateReads(DATETIMEZONE), JodaWrites.jodaDateWrites(DATETIMEZONE))
  implicit val LocalDateFormat: Format[LocalDate] =
    Format(JodaReads.jodaLocalDateReads(DATETIMEZONE), JodaWrites.jodaLocalDateWrites(DATETIMEZONE))
}
